#ifdef WX_PRECOMP
#include "../wx_pch.h"
#else
#include <wx/wx.h>
#endif //WX_PRECOMP

#include "component_utility.h"
#include "../race_item.h"
#include "../misc_func.h"

CellDataItem ComponentUtility::Create(const ComponentItem& comp, const RaceItem& race, const CellsType& type, bool selected)
{
  CellDataItem newItem;
  newItem.Type = type;
  newItem.Selected = selected;
  newItem.RaceID = race.ID;
  newItem.ComponentID = comp.ID;
  newItem.MinComponentLevel = GetComponentMinTechLevel(race, comp) - comp.RequredTehnLevel;
  newItem.MaxComponentLevel = GetComponentMaxTechLevel(race, comp) - comp.RequredTehnLevel;
  newItem.ComponentLevel = (comp.Type == TYPE_TRANSPORT) ? newItem.MinComponentLevel : newItem.MaxComponentLevel;

  return newItem;
}

bool ComponentUtility::Upgrade(CellDataItem& source, const ComponentItem& comp, const RaceItem& race)
{
  wxCHECK_MSG(source.ComponentID == comp.ID, true, wxT("Different component given"));
  if (source.RaceID != race.ID)
  {
    return true;
  }

  source.MinComponentLevel = GetComponentMinTechLevel(race, comp) - comp.RequredTehnLevel;
  source.MaxComponentLevel = GetComponentMaxTechLevel(race, comp) - comp.RequredTehnLevel;
  source.ComponentLevel = std::min(source.ComponentLevel, source.MaxComponentLevel);
  source.ComponentLevel = std::max(source.ComponentLevel, source.MinComponentLevel);
  if (source.MinComponentLevel < 0 || source.MaxComponentLevel <0)
  {
    source.ComponentID = source.RaceID = 0;
    source.ComponentLevel = source.MaxComponentLevel = source.MinComponentLevel = 0;
    return false;
  }

  return true;
}

